import { Controller } from 'egg';
// import * as path from 'path';

export default class HomeController extends Controller {
  public async index() {
    const { ctx } = this;
    ctx.body = '';
  }

  public async smsSj() {
    this.ctx.service.sj.sjsms().catch((error) => {
      this.logger.error('smsSj_error', error);
    });
    this.ctx.sendSuccess();
  }
  public async smstop() {
    return this.ctx.service.sj.sjsmsTop5();
  }

  public async smsdetail() {
    return this.ctx.service.sjDetail.sjsmsDetail();
  }

  public async exportNote() {
    return this.ctx.service.export.exportAppNote.exportNote();
  }

  public async sjsmsTop10() {
    return this.ctx.service.sjsmsTop.sjsmsTop10();
  }

  public async callSj() {
    this.ctx.service.sjCall.sjcall().catch((error) => {
      this.logger.error('callSj_error', error);
    });
    this.ctx.sendSuccess();
  }

  public async calltj() {
    this.ctx.service.tjCall.tjcall().catch((error) => {
      this.logger.error('calltj_error', error);
    });
    this.ctx.sendSuccess();
  }

  public async callsjphone() {
    await this.ctx.service.sjCallPhone.sjcall().catch((error) => {
      this.logger.error('callsjphone_error', error);
    });
    this.ctx.sendSuccess();
  }

  public async addData() {
    const { ctx } = this;
    try {
      await ctx.service.cdrMock.initMockData();
      await ctx.service.cdrMock.startMock();
    } catch (e) {
      const msg = 'addData error: ' + e;
      ctx.logger.error(msg);
      ctx.result.message = 'addData error: ' + e.message;
      return ctx.sendResult();
    }
    ctx.result.success = true;
    ctx.result.message = 'addData Success';
    return ctx.sendResult();
  }
  /**
   * 历史数据增加接口
   */
  public async insertDataByTime() {
    const {ctx} = this;
    let bizData = ctx.request.body.data;
    bizData = JSON.parse(bizData);
    const startTime = bizData.startTime;
    const endTime = bizData.endTime;
    ctx.logger.info('time', bizData, startTime, endTime);
    if (!startTime && !endTime) {
      ctx.result.success = false;
      ctx.result.message = 'not found date param!';
      ctx.sendResult();
      return;
    } else if (startTime.length !== endTime.length) {
      ctx.result.success = false;
      ctx.result.message = 'date param is error!';
      ctx.sendResult();
      return;
    }
    try {
      await ctx.service.cdrMock.initMockData();
      const count = await ctx.service.cdrMock.startMockByTime(startTime, endTime);
      ctx.result = {
        success: true,
        message: 'ok',
        data: {
          count,
        },
      };
      ctx.sendResult();
    } catch (e) {
      ctx.logger.error('insertDataByTime error');
      ctx.result.message = 'insertDataByTime error: ' + e.message;
      ctx.result.success = false;
      ctx.sendResult();
    }
  }

  public async accountDataCopy() {
    const { ctx } = this;
    const { originalAccount, account, pbx, sessionId, originalPBX } = ctx.request.body;
    try {
      await ctx.service.accountDataCopy.accountDataCopy({ originalAccount, originalPBX, account, pbx, sessionId });
      ctx.setSuccess(true);
    } catch (err) {
      ctx.logger.error(err);
      ctx.setSuccess(false);
    }
    ctx.sendResult();
  }

  /**
   * 查询年度呼入外呼数据
   */
  public async queryYearInboundOutbound() {
    const { ctx } = this;
    const { account, year } = ctx.request.body;
    if (!year) {
      return ctx.sendError('year is not provided');
    }
    try {
      const doc1 = await ctx.service.report.aggregateInboundTimeLength({ account, year });
      const doc2 = await ctx.service.report.aggregateInboundCount({ account, year });
      const doc3 = await ctx.service.report.aggregateOutbound({ account, year });
      ctx.setData({ ...doc1, ...doc2, ...doc3 });
      ctx.setSuccess();
    } catch (err) {
      ctx.logger.error(err);
    }
    ctx.sendResult();

  }
  public async importCallLabel() {
    const data = await this.ctx.service.call.importCallLabel();
    this.ctx.setData(data);
    this.ctx.setSuccess();
    this.ctx.sendResult();
  }
}
